package com.sun.services.impl;

import com.sun.entity.Lab;
import com.sun.entity.LabEquipmentCount;
import com.sun.entity.User;
import com.sun.mapper.LabMapper;
import com.sun.services.LabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wst
 * @create 2021-02-06
 */

@Service(value = "labServiceImpl")
public class LabServiceImpl implements LabService {


    @Autowired
    LabMapper labMapper;

    @Override
    public List<Lab> getLabs() {
        List<Lab> labs = labMapper.getLab();

        return labs;
    }

    @Override
    public Map<String, Object> statisticsById(User user, String labId) {

        if (labId == null) {
            labId = new String();
        }

        int id = 0;

        try {
            id = Integer.parseInt(labId);
        } catch (NumberFormatException numberFormatException) {
        }

        List<LabEquipmentCount> statisticsInfo = labMapper.getStatisticsInfo(id);
        Map<String, Object> labInfo = new HashMap<String, Object>();
        int totalCount = labMapper.getLabEquipmentTotalCount(id);
        labInfo.put("totalCount", totalCount);

        if (statisticsInfo == null) {

            return null;
        } else {
            List<String> equipmentNameList = new ArrayList<String>();
            List<String> equipmentCountList = new ArrayList<String>();

            for (LabEquipmentCount labEquipmentCount : statisticsInfo) {
                equipmentNameList.add("\'" + labEquipmentCount.getEquipmentName() + "\'");
                equipmentCountList.add(String.valueOf(labEquipmentCount.getCount()));
                totalCount -= labEquipmentCount.getCount();
            }

            if (statisticsInfo.size() >= 9 ) {
                equipmentNameList.add("\'其他\'");
                LabEquipmentCount qita = new LabEquipmentCount();
                qita.setCount(totalCount);
                qita.setEquipmentName("其他");
                statisticsInfo.add(qita);
            }

            labInfo.put("names", equipmentNameList.toArray());
            labInfo.put("counts", statisticsInfo.toArray());
            labInfo.put("labId", id);

            Lab lab = labMapper.getLabInfoById(id);
            List<Lab> labList = labMapper.getLab();
            labInfo.put("lab", lab);
            labInfo.put("labs", labList);

            return labInfo;
        }
    }

}

